package my.algorithms;

public class Strings
{
    public static boolean detectPalindrome(String aString)
    {
           char string [] = aString.toCharArray();
           int a = 0;
           int b = aString.length() - 1;

           while(a < b)
           {
               if(string[a] != string[b])
               {
                   return false;
               }
               ++a;
               --b;
           }
           return true;
    }


    public static String reverseStr(String aString)
    {
           char string [] = aString.toCharArray();
           int a = 0;
           int b = aString.length() - 1;

           while(a < b)
           {
                   char temp = string[a];
                   string[a] = string[b];
                   string[b] = temp;
                   ++a;
                   --b;
           }
           return new String(string);
    }

    //do itoa
    
    //do atoi
    
    public static int atoi(String string)
    {
        char[] str = string.toCharArray();

        int sign = 1;
        int i = 0;
        if ('-' == str[0])
        {
            sign = -1;
            i = 1;
        }

        int theNumber = 0;
        for (; i < str.length; i++)
        {
            if (str[i] > '0' && str[i] <= '9')
            {
                int tmp = (str[i] - '0'); // 0 is the 0 index
                theNumber = (theNumber * 10) + tmp; // to enlarge the number by
                                                    // *10 every time
            }
        }

        theNumber *= sign;
        return theNumber;
    }

    public static String itoa(int x)
    {
        boolean negative = false;
        String s = "";
        if (x == 0)
        {
            return "0";
        }

        negative = (x < 0);

        if (negative)
        {
            x = -1 * x;
        }

        while (x != 0)
        {
            // Assumes ASCII values, a character map is safer
            s = (char) (48 + (x % 10)) + s; // add char to front of s
            x = x / 10; // integer division gives quotient
        }

        if (negative)
        {
            s = "-" + s;
        }

        return s;
    } // convert

    // substring

    public static int stringInString(String string, String subString)
    {
        int strLen = string.length();
        int subLen = subString.length();

        if (strLen >= subLen && subLen > 0)
        {
            int max = (strLen - subLen);
            for (int i = 0; i <= max; i++)
            {
                if (string.charAt(i) == subString.charAt(0))
                {
                    for (int j = 1; j < subLen; j++)
                    {
                        if (string.charAt(i + j) != subString.charAt(j))
                        {
                            break;
                        } else if (j == subLen - 1)
                        {
                            return i;
                        }
                    }
                }
            }
        }
        return -1;
    }
}
